namespace GoPlay.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;
    using WebMatrix.WebData;

    internal sealed class Configuration : DbMigrationsConfiguration<GoPlay.Models.GoPlayDbContext> 
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
        }

        protected override void Seed(GoPlay.Models.GoPlayDbContext context)
        {
            SeedMembership();

            GoPlay.Models.Team t = new Models.Team { Id = 1, Name = "Ringers (Summer 2012)", UserId = 1 };
            context.Teams.AddOrUpdate(t);
            t = new Models.Team { Id = 2, Name = "Ringers (Fall 2012)", UserId = 1 };
            context.Teams.AddOrUpdate(t);
            t = new Models.Team { Id = 3, Name = "Ringers (Fall 2013)", UserId = 1 };
            context.Teams.AddOrUpdate(t);

            t = new Models.Team { Id = 4, Name = "Gunga (Fall 2012)", UserId = 2 };
            context.Teams.AddOrUpdate(t);
            t = new Models.Team { Id = 5, Name = "Gunga (Fall 2013)", UserId = 2 };
            context.Teams.AddOrUpdate(t);

            t = new Models.Team { Id = 6, Name = "Gunga (Spring 2011)", UserId = 2 };
            context.Teams.AddOrUpdate(t);
            t = new Models.Team { Id = 7, Name = "Gunga (Fall 2010)", UserId = 2 };
            context.Teams.AddOrUpdate(t);

            Models.Game g = new Models.Game { Id = 1, UserId = 1, Date = DateTime.Now.ToString("yyyy-MM-dd"), Time = "9:30 PM", TeamId = 1 };
            context.Games.AddOrUpdate(g);
            g = new Models.Game { Id = 2, UserId = 1, Date = DateTime.Now.ToString("yyyy-MM-dd"), Time = "9:45 PM", TeamId = 1 };
            context.Games.AddOrUpdate(g);
            g = new Models.Game { Id = 3, UserId = 1, Date = DateTime.Now.ToString("yyyy-MM-dd"), Time = "10:45 PM", TeamId = 1 };
            context.Games.AddOrUpdate(g);
            g = new Models.Game { Id = 4, UserId = 2, Date = DateTime.Now.ToString("yyyy-MM-dd"), Time = "8:15 PM", TeamId = 6 };
            context.Games.AddOrUpdate(g);

            Models.Contact c = new Models.Contact { Id = 1, Name = "Harry Smith", Email = "harry@inter.net", Nickname = "Balls", UserId = 1 };
            context.Contacts.AddOrUpdate(c);

            c = new Models.Contact { Id = 2, Name = "Jonny Grapes", Email = "jonny@inter.net", Nickname = "Vino", UserId = 1 };
            context.Contacts.AddOrUpdate(c);

            c = new Models.Contact { Id = 3, Name = "Homer Simpson", Email = "homer@inter.net", UserId = 1 };
            context.Contacts.AddOrUpdate(c);

            c = new Models.Contact { Id = 4, Name = "Tony Fingers", Email = "fing@inter.net", Nickname = "Digits", UserId = 2 };
            context.Contacts.AddOrUpdate(c);

            c = new Models.Contact { Id = 5, Name = "Sally Struthers", Email = "sally@inter.net", Nickname = "Gargoyle", UserId = 2 };
            context.Contacts.AddOrUpdate(c);

            c = new Models.Contact { Id = 6, Name = "Kevin Hoffman", Email = "hoff@inter.net", Nickname = "Hoff", UserId = 2 };
            context.Contacts.AddOrUpdate(c);

            Models.Player p = new Models.Player { Id = 1, ContactId = 1, UserId = 1, TeamId = 1, Goals = 1, Assists = 2, Points = 3 };
            context.Players.AddOrUpdate(p);
        }

        private void SeedMembership()
        {
            WebSecurity.InitializeDatabaseConnection("GoPlayDB", "UserProfile", "UserId", "UserName", autoCreateTables: false);
            if (!WebSecurity.UserExists("chipirek"))
                WebSecurity.CreateUserAndAccount("chipirek", "lollip0p",
                            new
                            {
                                FullName = "Charles Irek",
                                Email = "chip.irek@gmail.com"
                            }, false);

            if (!WebSecurity.UserExists("richardtooke"))
                WebSecurity.CreateUserAndAccount("richardtooke", "baconeggcheese",
                            new
                            {
                                FullName = "Richard Tooke",
                                Email = "r2k@me.com"
                            }, false);
        }
    }
}
